package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommunityShopDao;
import cn.hs.ph.dao.impl.CommunityShopDaoImpl;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/communityShop")
public class CommunityShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CommunityShopDao communityShopDao = new CommunityShopDaoImpl();
        if("select".equals(action)){
            Map<String,String> map = new HashMap<>();
            String communityId = req.getParameter("communityId");
            String shopName = req.getParameter("shopName");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("communityId",communityId);
            map.put("shopName",shopName);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            List<Shop> list = communityShopDao.selectAll(map);
            int total = communityShopDao.selectCount(map);
            Map<Object,Object> map1 = new HashMap<>();
            map1.put("list",list);
            map1.put("total",total);
            //数据传到页面
            Result result = Result.success(map1);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("delete".equals(action)){
//            String id = req.getParameter("communityId");
//            int communityId = 0;
//            if(id!=null && !"".equals(id)){
//                communityId = Integer.parseInt(id);
//            }
//            String id = req.getParameter("id");
            String id = req.getParameter("shopId");
            int shopId=0;
            if(id!=null && !"".equals(id)){
                shopId=Integer.parseInt(id);
            }
            int num = communityShopDao.delete(shopId);
            Result result = null;
            if (num!=0){
                result = Result.success("删除成功");
            }else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("add".equals(action)){
            //panghuCommunityId,fendeShopCommunityList
            String id = req.getParameter("panghuCommunityId");
            String ssid = req.getParameter("fendeShopCommunityList");
            String[] sid = ssid.split(",");
            int panghuCommunityId = 0;
            if (id!=null && !"".equals(id)){
                panghuCommunityId = Integer.parseInt(id);
            }
            boolean temp = false;
            for (String shopId : sid){
                int num = communityShopDao.addShop(Integer.parseInt(shopId), panghuCommunityId);
                if (num!=0){
                    temp=true;
                }else {
                    temp=false;
                }
            }
            Result result = null;
            if(temp){
                result = Result.success("添加成功");
            }else {
                result = Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }
}
